﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Cannonball
{
    class Scoreboard
    {

        private static int FindNumbersInString(string input)
        {
            string number = string.Empty;
            for (int i = 0; i < input.Length; i++)
            {
                if (char.IsDigit(input[i]))
                {
                    number += input[i];
                }
            }
            return int.Parse(number);
        }

        private static int Find10thMember(string fileLocation)
        {
            List<string> file = new List<string>();
            StreamReader reader = new StreamReader(fileLocation);
            using (reader)
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    file.Add(line);
                    line = reader.ReadLine();
                }
            }
            string member10;

            if (file.Count == 0 || file.Count < 10)
            {
                return -1;
            }
            else
            {
                member10 = file[file.Count - 1];
                return FindNumbersInString(member10);
            }
        }

        //Method adds new member to top10
        private static void ScoresAdd(int score)
        {
            List<string> file = new List<string>();
            List<string> sortedFile = new List<string>();
            StreamWriter scores = new StreamWriter(@"..\..\scores.txt", true);
            string topName;

            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(String.Format("{0," + 51 + "}", "You made it to the top10!"));
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write(String.Format("{0," + 50 + "}", "Please enter your name: "));

            topName = Console.ReadLine();

            using (scores)
            {
                scores.Write("{0} points - ", score);
                scores.WriteLine(topName);
            }


        }


        //SORT METHOD
        public void SortScores(string fileLocation)
        {
            List<int> numbers = new List<int>();
            List<string> names = new List<string>();
            StreamReader reader = new StreamReader(fileLocation);

            using (reader)
            {
                StringBuilder number = new StringBuilder();
                StringBuilder word = new StringBuilder();
                string line = reader.ReadLine();

                while (line != null)
                {
                    for (int i = 0; i < line.Length; i++)
                    {

                        if (char.IsDigit(line[i]))
                        {
                            number.Append(line[i]);
                        }
                        else
                        {
                            word.Append(line[i]);
                        }
                    }
                    numbers.Add(int.Parse(number.ToString()));
                    names.Add(word.ToString());
                    number.Clear();
                    word.Clear();
                    line = reader.ReadLine();

                }
            }

            //selection sort
            int startingPosition = 0;
            while (startingPosition <= numbers.Count - 1)
            {
                int currentMinIndex = 0;
                int currentMin = int.MaxValue;
                string currentMinString = string.Empty;

                for (int i = startingPosition; i < numbers.Count; i++)
                {
                    if (numbers[i] < currentMin)
                    {
                        currentMin = numbers[i];
                        currentMinString = names[i];
                        currentMinIndex = i;
                    }
                }
                int temp = numbers[startingPosition];
                string tempString = names[startingPosition];

                numbers[startingPosition] = currentMin;
                names[startingPosition] = currentMinString;

                numbers[currentMinIndex] = temp;
                names[currentMinIndex] = tempString;
                startingPosition++;
            }
            numbers.Reverse();
            names.Reverse();
            StreamWriter writer = new StreamWriter(fileLocation);
            using (writer)
            {
                for (int i = 0; i < numbers.Count; i++)
                {

                    writer.Write(numbers[i]);
                    writer.WriteLine(names[i]);
                }
            }

        }


        //PRINTS THE SCORE
        public void PrintScore()
        {
            List<string> sortedScores = new List<string>();
            StreamReader reader = new StreamReader(@"..\..\scores.txt");

            using (reader)
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    sortedScores.Add(line);
                    line = reader.ReadLine();
                }
            }


            Console.ForegroundColor = ConsoleColor.DarkYellow;
            int leftOffSet = (Console.WindowWidth / 2) - 14;
            int topOffSet = (Console.WindowHeight / 2) - 15;

            Console.SetCursorPosition(leftOffSet, topOffSet);
            Console.WriteLine("TOP 10 SCORES: ");
            topOffSet += 3;

            for (int i = 0; i < sortedScores.Count; i++)
            {

                Console.SetCursorPosition(leftOffSet, topOffSet);
                Console.WriteLine("{0}. {1}", i + 1, sortedScores[i]);
                topOffSet += 2;
            }
        }

        public void GameOver(int score)
        {
            //IN TOP10
            if (score > Find10thMember(@"..\..\scores.txt"))
            {
                ScoresAdd(score);
                SortScores(@"..\..\scores.txt");
                PrintScore();
            }
            else
            //NOT IN TOP10
            {
                int topOffSet = (Console.WindowHeight / 2) - 17;
                Console.SetCursorPosition(0, topOffSet);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(String.Format("{0," + 43 + "}", "GAME OVER!"));
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine(String.Format("{0," + 50 + "}", "Your score is: " + score + " points"));

                ;
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine(String.Format("{0," + 51 + "}", "1. View the Top10 scores!"));
                Console.WriteLine();
                Console.WriteLine(String.Format("{0," + 33 + "}", "2. EXIT"));
                while (true)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                    if (pressedKey.Key == ConsoleKey.D1 || pressedKey.Key == ConsoleKey.NumPad1)
                    {
                        Console.Clear();
                        SortScores(@"..\..\scores.txt");
                        PrintScore();
                    }
                    if (pressedKey.Key == ConsoleKey.D2 || pressedKey.Key == ConsoleKey.NumPad2)
                    {
                        Environment.Exit(0);
                    }
                }

            }

        }
    }
}
